//
//  YYNavigationBarButton.m
//  YYNavigationBar
//
//  Created by yangyang on 16/8/2.
//  Copyright © 2016年 机智的静默( http://www.cnblogs.com/jingmo/ ). All rights reserved.
//

#import "YYNavigationBarButton.h"
#import "YYNavigationMacro.h"
#import "UIView+YYNavigationFrame.h"

typedef NS_ENUM(NSInteger, YYNavigationBarButtonType) {
    
    YYNavigationBarButtonUnknow = 0,
    YYNavigationBarButtonText,
    YYNavigationBarButtonImage,
    YYNavigationBarButtonAll,
};

@interface YYNavigationBarButton ()

@property (nonatomic, copy) YYNavigationBarButtonTapHandler handler;

@end

@implementation YYNavigationBarButton

#pragma mark - Init Method

+ (instancetype)buttonWithTitle:(NSString *)title handler:(YYNavigationBarButtonTapHandler)handler {
    NSAssert(title != nil, @"文字不能为空");
    return [YYNavigationBarButton __buttonWithImage:nil title:title handler:handler type:YYNavigationBarButtonText];
}

+ (instancetype)buttonWithImage:(UIImage *)image handler:(YYNavigationBarButtonTapHandler)handler {
    NSAssert(image != nil, @"图片不能为空");
    return [YYNavigationBarButton __buttonWithImage:image title:nil handler:handler type:YYNavigationBarButtonImage];
}

+ (instancetype)buttonWithImage:(UIImage *)image title:(NSString *)title handler:(YYNavigationBarButtonTapHandler)handler {
    NSAssert(image != nil, @"图片不能为空");
    NSAssert(title != nil, @"文字不能为空");
    return [YYNavigationBarButton __buttonWithImage:image title:title handler:handler type:YYNavigationBarButtonAll];
}

- (void)refreshSize {
    [self sizeToFit];
    self.width = self.width + 1;
}

#pragma mark - Action Method

- (void)btnTap:(UIButton *)sender {
    if (self.handler) {
        self.handler(sender);
    }
}

#pragma mark - Private Method

+ (instancetype)__buttonWithImage:(UIImage *)image title:(NSString *)title handler:(YYNavigationBarButtonTapHandler)handler type:(YYNavigationBarButtonType)type {
    
    YYNavigationBarButton *button = [YYNavigationBarButton buttonWithType:UIButtonTypeCustom];
    if (type == YYNavigationBarButtonText) {
        [button setTitle:title forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:YYButtonFont]];
    } else if (type == YYNavigationBarButtonImage) {
        [button setImage:image forState:UIControlStateNormal];
    } else if (type == YYNavigationBarButtonAll) {
        [button setTitle:title forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:YYButtonFont]];
        [button setImage:image forState:UIControlStateNormal];
    }
    [button sizeToFit];
    button.handler = handler;
    [button addTarget:button action:@selector(btnTap:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

#pragma mark - Super Method

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    CGRect bounds = self.bounds;
    CGFloat widthDelta = MAX(44.0 - self.width, 0);
    CGFloat heightDelta = MAX(44.0 - self.height, 0);
    bounds = CGRectInset(bounds, - 0.5 * widthDelta, - 0.5 * heightDelta);
    return CGRectContainsPoint(bounds, point);
}

@end
